package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.R;
import com.wude.guild.dialog.PayDialog;
import com.wude.guild.dialog.PaySuccessDialog;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/22 0022.
 * <p>
 * 电信活动
 */

public class PayTelecomFragment extends TitleBaseFragment implements View.OnClickListener {
    private TextView tv_phone;//电话号码
    private TextView tv_commit;//立即充值
    public static PayDialog dialog;
    private String tag;
    private String phone;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("PayTelecomFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("PayTelecomFragment");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate;
        if ("unicom".equals(tag))
            inflate = inflater.inflate(R.layout.pay_unicom_fragment, container, false);
        else
            inflate = inflater.inflate(R.layout.get_tariffe_telecom_fragment, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View view) {
        tv_phone = view.findViewById(R.id.tv_phone);
        tv_commit = view.findViewById(R.id.tv_commit);

        tv_commit.setOnClickListener(this);
        tv_phone.setText(phone + "");
    }

    @Override
    public void onClick(View view) {
        //充值的弹窗
        String s = tv_phone.getText().toString();
        if (TextUtils.isEmpty(s)) {
            ToastUtil.showToast("手机号获取有误");
            return;
        }

        switch (tag) {
            case "telecom"://电信
                dialog = new PayDialog(getActivity(), s, getActivity());
                dialog.show();
                break;
            case "unicom"://联通
                //领取话费
                Map<String, String> dataMap = new HashMap<>();
                dataMap.put("token", Utils.getLoginUser().token);
                HttpCom.POST(handler, HttpCom.getReceiveCalls(), dataMap, false);
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        if (1 == status) {
                            //领取成功
                            PaySuccessDialog dialog = new PaySuccessDialog(getContext(), getActivity(), tv_phone.getText().toString());
                            dialog.show();
                        } else {
                            ToastUtil.showToast(jsonObject.getString("return_code") + "");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtils.showLongToast("网络异常！");
                    break;
            }
        }
    };

    @Override
    public void onEnter(Object data) {
        super.onEnter(data);
        Map<String, String> dataMap = (Map<String, String>) data;
        tag = dataMap.get("tag");
        phone = dataMap.get("phone");
    }
}
